
#ifndef RT_WIN32
 #error ERROR: only WIN32 supports NTLM!
#endif

#ifndef RTHTTPNTLMAUTH_H
#define RTHTTPNTLMAUTH_H

#include "RtHttpAuthenticator.h"

#define SECURITY_WIN32
#include "sspi.h"
#include "issperr.h"

class CRtHttpNTLMAuth : public CRtHttpAuthSteps
{
public:
	CRtHttpNTLMAuth();
	virtual ~CRtHttpNTLMAuth();

	virtual CRtString do_GenerateCredentials(
		IRtChannelHttpClient *aChannel, 
		LPCSTR aChallenge, 
		LPCSTR aUsername, 
		LPCSTR aPassword);

	virtual BOOL AreCredentialsReusable();
	virtual BOOL AllowFirstTryWithoutUserName();
	virtual LPCSTR GetAuthenticateScheme();

	static LPCSTR s_pszNTLMAuth;

private:
	BOOL InitAuthorizationHeader();
	
	typedef struct _SEC_FUNC {
		FREE_CREDENTIALS_HANDLE_FN pFreeCredentialsHandle;
		ACQUIRE_CREDENTIALS_HANDLE_FN pAcquireCredentialsHandle;
		QUERY_SECURITY_PACKAGE_INFO_FN pQuerySecurityPackageInfo;   // A
		FREE_CONTEXT_BUFFER_FN pFreeContextBuffer;
		INITIALIZE_SECURITY_CONTEXT_FN pInitializeSecurityContext;  // A
		COMPLETE_AUTH_TOKEN_FN pCompleteAuthToken;
		ENUMERATE_SECURITY_PACKAGES_FN pEnumerateSecurityPackages;  // A
		DWORD dwMaxToken;
	} SEC_FUNC;

	typedef struct _AUTH_SEQ {
		BOOL _fNewConversation;
		CredHandle _hcred;
		BOOL _fHaveCredHandle;
		DWORD _cbMaxToken;
		BOOL _fHaveCtxtHandle;
		struct _SecHandle  _hctxt;
		BOOL _fUUEncodeData;
	} AUTH_SEQ;

	AUTH_SEQ m_asAuth;
	static SEC_FUNC s_sfProcs;
	static HINSTANCE s_hSecLib;
};

#endif // !RTHTTPNTLMAUTH_H
